package com.lanelu.hencoderplusstudy.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import androidx.core.content.res.ResourcesCompat
import com.lanelu.hencoderplusstudy.R
import com.lanelu.lib_common.utils.LogUtils

private var RADIUS = 0f

class SportView(context: Context, attrs: AttributeSet?) : View(context, attrs) {

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        textSize = 100f.dp
        typeface = ResourcesCompat.getFont(context, R.font.satisfy_regular)
        textAlign = Paint.Align.CENTER
    }//抗锯齿
    private val path = Path()
    private var rectF = RectF()
    private val bounds = Rect()
    private val fontMetrics = Paint.FontMetrics()//字体度量

    init {
        paint.color = Color.GRAY
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 20f.dp
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        LogUtils.e("onSizeChanged")
        RADIUS = width / 3f
        rectF = RectF(
            width / 2f - RADIUS,
            height / 2f - RADIUS,
            width / 2f + RADIUS,
            height / 2f + RADIUS
        )
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        LogUtils.e("onDraw")
        canvas.save()

        //绘制环
        paint.color = Color.GRAY
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 15f.dp
        canvas.drawCircle(width / 2f, height / 2f, RADIUS, paint)

        //绘制进度条
        paint.color = Color.RED
        paint.strokeCap = Paint.Cap.ROUND//圆点
        canvas.drawArc(rectF, -90f, 225f, false, paint)

        //绘制文字
        paint.textSize = 100.dp
        paint.style = Paint.Style.FILL
//        paint.getTextBounds("abab", 0, "abab".length, bounds)//静态文字考虑使用bounds进行上下边界的测量
        paint.getFontMetrics(fontMetrics)
        canvas.drawText(
            "aaaa",
            width / 2f,
            height / 2f - (paint.fontMetrics.ascent + paint.fontMetrics.descent) / 2f,
            paint
        )

        //绘制文字2
        //左边和上边的贴边
        paint.textSize = 150.dp
        paint.textAlign = Paint.Align.LEFT //文字居左
        paint.getFontMetrics(fontMetrics)
        paint.getTextBounds("abab", 0, "abab".length, bounds)
        canvas.drawText(
            "abab",
            -bounds.left.toFloat(),
//            -fontMetrics.top,//靠近顶部，需要把文字纵向的偏移量减掉（阅读性文字建议用这个）
            -bounds.top.toFloat(),//顶部贴边（观赏性文字可以用这个）不过一切都根据实际情况来定
            paint
        )

        //绘制文字3
        paint.textSize = 15.dp
        paint.getTextBounds("abab", 0, "abab".length, bounds)
        canvas.drawText("abab", -bounds.left.toFloat(), -bounds.top.toFloat(), paint)
//        canvas.drawText("abab", 0f, -fontMetrics.top, paint)

        //绘制文字4 贴右贴下
        paint.textSize = 15.dp
        paint.getTextBounds("cdcd", 0, "cdcd".length, bounds)
        canvas.drawText("cdcd", width.toFloat() - bounds.right.toFloat(), height.toFloat(), paint)
//        canvas.drawText("abab", 0f, -fontMetrics.top, paint)

        canvas.restore()
    }


}